package cn.mst.vrs.controller.admin.authtype;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.mst.vrs.controller.admin.authtype.vo.AuthTypeDictRespVO;
import cn.mst.vrs.controller.admin.authtype.vo.AuthTypePageReqVO;
import cn.mst.vrs.controller.admin.authtype.vo.AuthTypeRespVO;
import cn.mst.vrs.controller.admin.authtype.vo.AuthTypeSaveReqVO;
import cn.mst.vrs.convert.AuthTypeConvert;
import cn.mst.vrs.dal.dataobject.authtype.AuthTypeDO;
import cn.mst.vrs.service.authtype.AuthTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 认证类型")
@RestController
@RequestMapping("/vrs/auth-type")
@Validated
public class AuthTypeController {

    @Resource
    private AuthTypeService authTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建认证类型")
    @PreAuthorize("@ss.hasPermission('vrs:auth-type:create')")
    public CommonResult<Long> createAuthType(@Valid @RequestBody AuthTypeSaveReqVO createReqVO) {
        return success(authTypeService.createAuthType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新认证类型")
    @PreAuthorize("@ss.hasPermission('vrs:auth-type:update')")
    public CommonResult<Boolean> updateAuthType(@Valid @RequestBody AuthTypeSaveReqVO updateReqVO) {
        authTypeService.updateAuthType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除认证类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('vrs:auth-type:delete')")
    public CommonResult<Boolean> deleteAuthType(@RequestParam("id") Long id) {
        authTypeService.deleteAuthType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得认证类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('vrs:auth-type:query')")
    public CommonResult<AuthTypeRespVO> getAuthType(@RequestParam("id") Long id) {
        AuthTypeDO authType = authTypeService.getAuthType(id);
        return success(BeanUtils.toBean(authType, AuthTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得认证类型分页")
    @PreAuthorize("@ss.hasPermission('vrs:auth-type:query')")
    public CommonResult<PageResult<AuthTypeRespVO>> getAuthTypePage(@Valid AuthTypePageReqVO pageReqVO) {
        PageResult<AuthTypeDO> pageResult = authTypeService.getAuthTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AuthTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出认证类型 Excel")
    @PreAuthorize("@ss.hasPermission('vrs:auth-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAuthTypeExcel(@Valid AuthTypePageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AuthTypeDO> list = authTypeService.getAuthTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "认证类型.xls", "数据", AuthTypeRespVO.class,
                BeanUtils.toBean(list, AuthTypeRespVO.class));
    }

    @GetMapping("/list-dict")
    @Operation(summary = "类型字典")
    @PreAuthorize("@ss.hasAnyPermissions('vrs:auth-type:query')")
    public CommonResult<List<AuthTypeDictRespVO>> listDict() {
        List<AuthTypeDO> authTypeDOList = authTypeService.listAuthTypeDict();
        return CommonResult.success(AuthTypeConvert.INSTANCE.toDictRespVOList(authTypeDOList));
    }
}